package com.longcai.piaojia.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.longcai.piaojia.R;
import com.longcai.piaojia.adapter.LatestAdapter;
import com.longcai.piaojia.conn.LatestListGet;
import com.longcai.piaojia.dialog.WarnningDialog;
import com.longcai.piaojia.util.UtilPermission;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilApp;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by shenyuxin on 2017/1/8.
 */
public class LatestActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.latest_bottom_ll)
    private LinearLayout latest_bottom_ll;
    @BoundView(R.id.latest_phone_tv)
    private TextView latest_phone_tv;
    @BoundView(R.id.latest_lv)
    private PullToRefreshListView pullToRefreshListView;

    private ListView latest_lv;
    private List<LatestListGet.Latest> lists = new ArrayList<>();
    private LatestAdapter adapter;
    private LatestListGet.LatestEntity mLatestEntity;

    private LatestListGet latestListGet = new LatestListGet(new AsyCallBack<LatestListGet.LatestEntity>() {

        @Override
        public void onSuccess(String toast, int type, LatestListGet.LatestEntity latestEntity) throws Exception {

            if (type != 2) {

                lists.clear();

            }

            mLatestEntity = latestEntity;

            lists.addAll(latestEntity.latestList);

            adapter.notifyDataSetChanged();

            if (latestEntity.program_phone.length() > 0) {

                latest_phone_tv.setText(latestEntity.program_phone);
                latest_bottom_ll.setVisibility(View.VISIBLE);
            }


        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {

            pullToRefreshListView.onPullUpRefreshComplete();
            pullToRefreshListView.onPullDownRefreshComplete();

            if (type != 2) {

                setLastUpdateTime();
            }

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_latest);

        setBackTrue();

        setTitleName(getString(R.string.project_title_text));

        String[] permission = {Manifest.permission.CALL_PHONE};
        UtilPermission.Permission(this, permission);

        pullToRefreshListView.setScrollLoadEnabled(false);
        pullToRefreshListView.setPullLoadEnabled(true);
        pullToRefreshListView.setPullRefreshEnabled(true);

        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                latestListGet.page = 1;
                latestListGet.execute(context, false, 1);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                if (lists != null && lists.size() < mLatestEntity.total) {

                    latestListGet.page = mLatestEntity.current_page + 1;
                    latestListGet.execute(context, false, 2);

                } else {

                    pullToRefreshListView.onPullUpRefreshComplete();
                    pullToRefreshListView.onPullDownRefreshComplete();

                }


            }
        });

        latest_lv = pullToRefreshListView.getRefreshableView();

        latest_lv.setDivider(null);

        latest_lv.setAdapter(adapter = new LatestAdapter(context, lists));

        latest_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d(lists.get(0).weburl, "aaaaaaaaaaaaaaaaaaaaaaaaa");

                Log.d(lists.get(position).weburl, "bbbbbbbbbbbbbbbbbbbbbbbbbbb");
                if (lists.get(position).type.equals("1")) {


                    Intent intent = new Intent();
                    intent.putExtra("url", lists.get(position).weburl);
                    intent.setClass(LatestActivity.this, ChainActivity.class);
                    startActivity(intent);


                } else {

                    Bundle bundle = new Bundle();

                    bundle.putString("id", lists.get(position).id);
                    bundle.putString("title", lists.get(position).title);

                    startActivity(new Intent(LatestActivity.this, LatestDetailActivity.class).putExtras(bundle));
                }


            }
        });

        latestListGet.page = 1;
        latestListGet.execute(this);

        latest_bottom_ll.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.latest_bottom_ll:

                new WarnningDialog(context, mLatestEntity.program_phone, context.getString(R.string.cancel_text), context.getString(R.string.confirm_text)) {
                    @Override
                    protected void onConfirm() {

                        UtilApp.call(context, mLatestEntity.program_phone);

                        return;
                    }
                }.show();

                break;

        }

    }

    private void setLastUpdateTime() {

        SimpleDateFormat mDateFormat = new SimpleDateFormat("MM-dd HH:mm");
        pullToRefreshListView.setLastUpdatedLabel(mDateFormat.format(System.currentTimeMillis()));
    }

}
